package com.example.xmhds_1.mapper.userinfo;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.xmhds_1.entity.ChatMessage;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 好友聊天消息数据访问接口
 */
@Mapper
public interface ChatMessageMapper {

    /**
     * 保存聊天消息
     */
    @Insert("INSERT INTO chat_message (user_0, user_1, messages, deleted) " +
            "VALUES (#{user0}, #{user1}, #{messages}, #{deleted})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(ChatMessage chatMessage);

    /**
     * 获取两个用户之间的聊天记录
     */
    @Select("SELECT * FROM chat_message " +
            "WHERE (user_0 = #{user0} AND user_1 = #{user1} " +
            "OR user_0 = #{user1} AND user_1 = #{user0}) " +
            "AND deleted = 0")
    ChatMessage getMessages(@Param("user0") String user0, @Param("user1") String user1);

    /**
     * 获取用户的所有好友的聊天记录
     */
    @Select("SELECT * FROM chat_message " +
            "WHERE (user_0 = #{user0} OR user_1 = #{user0}) " +
            "AND deleted = 0")
    List<ChatMessage> getMessagesByUser(@Param("user0") String user0);

    /**
     * 逻辑删除聊天记录
     */
    @Update("UPDATE chat_message SET deleted = 1 WHERE id = #{id}")
    int deleteById(@Param("id") Long id);

    /**
     * 更新聊天记录
     */
    @Update("UPDATE chat_message SET messages = #{messages} WHERE id = #{id}")
    int updateById(ChatMessage chatMessage);
}
